package com.example.llm.sgg.chat.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.socket.WebSocketSession;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class WebSocketSessionManager {

    // 用户ID -> WebSocketSession
    private final Map<String, WebSocketSession> userSessions = new ConcurrentHashMap<>();

    // 绑定用户ID与会话
    public void bindUserSession(String userId, WebSocketSession session) {
        userSessions.put(userId, session);
    }

    // 解绑用户会话
    public void unbindUserSession(String userId) {
        userSessions.remove(userId);
    }

    // 获取用户会话
    public Optional<WebSocketSession> getSession(String userId) {
        return Optional.ofNullable(userSessions.get(userId));
    }

    // 获取所有在线用户
    public Collection<String> getOnlineUsers() {
        return userSessions.keySet();
    }
}
